package com.wande.dataplatform.filecollection.controller;

import com.wande.common.core.domain.R;
import com.wande.common.core.validate.AddGroup;
import com.wande.common.core.validate.EditGroup;
import com.wande.common.log.annotation.Log;
import com.wande.common.log.enums.BusinessType;
import com.wande.common.mybatis.core.page.PageQuery;
import com.wande.common.mybatis.core.page.TableDataInfo;
import com.wande.common.web.core.BaseController;
import com.wande.dataplatform.filecollection.domain.bo.CollectionRuleBo;
import com.wande.dataplatform.filecollection.domain.bo.CollectionTaskBo;
import com.wande.dataplatform.filecollection.domain.vo.CollectionRuleVo;
import com.wande.dataplatform.filecollection.domain.vo.CollectionTaskVo;
import com.wande.dataplatform.filecollection.domain.vo.FileRecordVo;
import com.wande.dataplatform.filecollection.service.IFileCollectionService;
import com.wande.dataplatform.filecollection.security.FileSecurityValidator;
import com.wande.dataplatform.filecollection.security.AuditLogService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 文件采集控制器
 *
 * @author wande
 */
@RestController
@RequestMapping("/dataplatform/file-collection")
@RequiredArgsConstructor
public class FileCollectionController extends BaseController {

    private final IFileCollectionService fileCollectionService;
    private final FileSecurityValidator securityValidator;
    private final AuditLogService auditLogService;

    /**
     * 查询采集规则列表
     */
    @GetMapping("/rule/list")
    public TableDataInfo<CollectionRuleVo> listRules(CollectionRuleBo bo, PageQuery pageQuery) {
        return fileCollectionService.listRules(bo, pageQuery);
    }

    /**
     * 查询采集规则详情
     */
    @GetMapping("/rule/{id}")
    public R<CollectionRuleVo> getRule(@PathVariable Long id) {
        return R.ok(fileCollectionService.getRule(id));
    }

    /**
     * 新增采集规则
     */
    @Log(title = "文件采集规则", businessType = BusinessType.INSERT)
    @PostMapping("/rule")
    public R<Long> addRule(@Validated(AddGroup.class) @RequestBody CollectionRuleBo bo) {
        Long ruleId = fileCollectionService.createRule(bo);
        
        // 记录审计日志
        Map<String, Object> details = new HashMap<>();
        details.put("ruleName", bo.getRuleName());
        details.put("fileType", bo.getFileType());
        details.put("sourceType", bo.getSourceType());
        auditLogService.logRuleOperation("CREATE", ruleId, details);
        
        return R.ok(ruleId);
    }

    /**
     * 修改采集规则
     */
    @Log(title = "文件采集规则", businessType = BusinessType.UPDATE)
    @PutMapping("/rule")
    public R<Void> editRule(@Validated(EditGroup.class) @RequestBody CollectionRuleBo bo) {
        fileCollectionService.updateRule(bo);
        
        // 记录审计日志
        Map<String, Object> details = new HashMap<>();
        details.put("ruleName", bo.getRuleName());
        auditLogService.logRuleOperation("UPDATE", bo.getId(), details);
        
        return R.ok();
    }

    /**
     * 删除采集规则
     */
    @Log(title = "文件采集规则", businessType = BusinessType.DELETE)
    @DeleteMapping("/rule/{id}")
    public R<Void> removeRule(@PathVariable Long id) {
        // 记录审计日志
        auditLogService.logRuleOperation("DELETE", id, new HashMap<>());
        
        fileCollectionService.deleteRule(id);
        return R.ok();
    }

    /**
     * 启用采集规则
     */
    @Log(title = "文件采集规则", businessType = BusinessType.UPDATE)
    @PutMapping("/rule/{id}/enable")
    public R<Void> enableRule(@PathVariable Long id) {
        fileCollectionService.enableRule(id);
        return R.ok();
    }

    /**
     * 禁用采集规则
     */
    @Log(title = "文件采集规则", businessType = BusinessType.UPDATE)
    @PutMapping("/rule/{id}/disable")
    public R<Void> disableRule(@PathVariable Long id) {
        fileCollectionService.disableRule(id);
        return R.ok();
    }

    /**
     * 手动触发采集任务
     */
    @Log(title = "文件采集任务", businessType = BusinessType.OTHER)
    @PostMapping("/rule/{ruleId}/trigger")
    public R<Long> triggerTask(@PathVariable Long ruleId) {
        return R.ok(fileCollectionService.triggerTask(ruleId));
    }

    /**
     * 查询采集任务列表
     */
    @GetMapping("/task/list")
    public TableDataInfo<CollectionTaskVo> listTasks(CollectionTaskBo bo, PageQuery pageQuery) {
        return fileCollectionService.listTasks(bo, pageQuery);
    }

    /**
     * 查询采集任务详情
     */
    @GetMapping("/task/{id}")
    public R<CollectionTaskVo> getTask(@PathVariable Long id) {
        return R.ok(fileCollectionService.getTask(id));
    }

    /**
     * 停止采集任务
     */
    @Log(title = "文件采集任务", businessType = BusinessType.OTHER)
    @PutMapping("/task/{id}/stop")
    public R<Void> stopTask(@PathVariable Long id) {
        fileCollectionService.stopTask(id);
        return R.ok();
    }

    /**
     * 重跑采集任务
     */
    @Log(title = "文件采集任务", businessType = BusinessType.OTHER)
    @PostMapping("/task/{id}/retry")
    public R<Long> retryTask(@PathVariable Long id) {
        return R.ok(fileCollectionService.retryTask(id));
    }

    /**
     * 查询文件记录列表
     */
    @GetMapping("/file/list")
    public TableDataInfo<FileRecordVo> listFileRecords(@RequestParam Long taskId, PageQuery pageQuery) {
        CollectionTaskBo bo = new CollectionTaskBo();
        bo.setId(taskId);
        return fileCollectionService.listFileRecords(bo, pageQuery);
    }

    /**
     * 查询文件记录详情
     */
    @GetMapping("/file/{id}")
    public R<FileRecordVo> getFileRecord(@PathVariable Long id) {
        return R.ok(fileCollectionService.getFileRecord(id));
    }

    /**
     * 下载文件
     */
    @GetMapping("/file/{id}/download")
    public R<String> downloadFile(@PathVariable Long id) {
        return R.ok(fileCollectionService.downloadFile(id));
    }
}
